"""
# -*- coding: utf-8 -*-
Time    : 2025/7/28 下午3:46
Author  : Micah
File    : test_tag_method.py
Function:
"""
import pytest
import requests
import yaml


# 使用参数化方式执行多次
@pytest.mark.parametrize('value', yaml.safe_load(open('test_tag_method.yaml', 'r', encoding='utf-8')))
def test_create_tag(get_access_token, value):
    url = f'https://api.weixin.qq.com/cgi-bin/tags/create?access_token={get_access_token}'
    data = {"tag": {"name": value}}
    rep = requests.post(url, json=data)
    assert rep.status_code == 200
    assert rep.json()['tag']['name'] == value


@pytest.fixture()
def get_tag(get_access_token):
    url = f'https://api.weixin.qq.com/cgi-bin/tags/get?access_token={get_access_token}'
    rep = requests.get(url)
    # 从返回中获取最后1个id号
    ids = rep.json()['tags'][-1]['id']
    # 获取倒数第2个id号
    ids2 = rep.json()['tags'][-2]['id']
    ides = [ids, ids2]
    assert rep.status_code == 200
    assert 'tags' in rep.text
    return ides


def test_update_tag(get_access_token, get_tag):
    url = f'https://api.weixin.qq.com/cgi-bin/tags/update?access_token={get_access_token}'
    data1 = {"tag": {"id": get_tag[0], "name": "只觉388"}}
    data2 = {"tag": {"id": get_tag[1], "name": "zenm338"}}
    rep1 = requests.post(url, json=data1)
    assert rep1.status_code == 200
    assert rep1.json()['errcode'] == 0
    rep2 = requests.post(url, json=data2)
    assert rep2.status_code == 200
    assert rep2.json()['errcode'] == 0


def test_udelete_tag(get_access_token, get_tag):
    url = f'https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={get_access_token}'
    print(get_tag)
    data1 = {"tag": {"id": get_tag[0]}}
    data2 = {"tag": {"id": get_tag[1]}}
    rep1 = requests.post(url, json=data1)
    assert rep1.status_code == 200
    assert rep1.json()['errcode'] == 0
    rep2 = requests.post(url, json=data2)
    assert rep2.status_code == 200
    assert rep2.json()['errcode'] == 0


if __name__ == '__main__':
    pytest.main()
